home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / network / pktdrvtp.zip / TRAFMON.ZIP / TRAFMON.PAS < prev    next >
Pascal/Delphi Source File  |  1993-09-28  |  13KB  |  352 lines

  1. Program TrafficMonitor;
  2. {
  3. ╔════════════════════════════════════════════════════════════════════════╗
  4. ║ TRAFMON Version 1.0  (Released to public domain)                       ║
  5. ╟────────────────────────────────────────────────────────────────────────╢
  6. ║ Author : O. Rehmann                                                    ║
  7. ║                                                                        ║
  8. ║ e-mail : 100016.732@compuserve.com                                     ║
  9. ║        : CZ8OR@zcvx00.decnet.ascom.ch                                  ║
  10. ╟────────────────────────────────────────────────────────────────────────╢
  11. ║ Sample program to monitor ethernet traffic.                            ║
  12. ╟────────────────────────────────────────────────────────────────────────╢
  13. ║ ATTENTION:                                                             ║
  14. ║                                                                        ║
  15. ║ The author can not be held responsible for any damages resulting out   ║
  16. ║ of the use of this software !!!                                        ║
  17. ║                                                                        ║
  18. ╚════════════════════════════════════════════════════════════════════════╝
  19. }
  20. {$A+,B-,D+,E-,F+,G-,I-,L+,N-,O-,R-,S-,V+,X+}
  21. {$M $2048 $0 $8192}
  22.  
  23. USES CRT,DOS,STRINGS,TIMER,PKTDRVR;
  24.  
  25. CONST  MaxEthernetCapacity = 1250000; { Max. theroetical ethernet capacity }
  26.  
  27. TYPE
  28.        { 6 Byte ethernet address }
  29.        EthernetAddr = Array[00..05] of Byte;
  30.  
  31.        MACHeader    = RECORD { IEEE 802.3 header }
  32.                         DestAddr   : EthernetAddr;
  33.                         SourceAddr : EthernetAddr;
  34.                         TypeLen    : Word;
  35.                       END;
  36.  
  37. VAR Key             : Char;
  38.  
  39.     pktDriver       : TPKTDRVR;    { Instance of the TPKTDRVR object         }
  40.     pktDriverInfo   : TDRVRINFO;   { record for driver information           }
  41.     pktDriverAccess : TACCESSTYPE; { record used for accessing packet driver }
  42.  
  43.     TypeField       : Word;
  44.  
  45.     RcvPacket       : Array[00..1524] of Byte; { Rcv buffer       }
  46.     RcvLength       : Word;                    { Length of packet }
  47.     RcvHeader       : MACheader;               { Header           }
  48.  
  49.     PacketCount     : Word;                    { Packet counter   }
  50.     PacketDelay     : Word;
  51.  
  52.     CurTraffic      : Real;
  53.     CumTraffic      : LongInt;
  54.     VUMeterLen      : Real;
  55.     MyTimer         : _TIMER;
  56.  
  57.     I               : Integer;
  58.  
  59. { ========================================================================== }
  60. { The receiver procedure:                                                    }
  61. {$S-}PROCEDURE pktReceiver; ASSEMBLER;
  62. ASM
  63.   PUSH AX                      { Push registers onto stack }
  64.   PUSH BX
  65.   PUSH CX
  66.   PUSH DX
  67.  
  68.  
  69.   CMP  AX,0001                 { AX=1 means frame copied }
  70.   JZ   @@FrameCopied
  71.   CMP  AX,0000                 { AX=0 means allocate memory please }
  72.   JZ   @@AllocMemory
  73.   JMP  @@EXIT                  { Invalid register contents for AX so exit}
  74.  
  75. @@AllocMemory:
  76.  
  77.   MOV  DX,0                    { ES:DI = 0000:0000, we don't want the packet }
  78.   MOV  ES,DX
  79.   MOV  DI,0                    { We don't grab the packet }
  80.  
  81.   MOV  DX,SEG PacketCount      { Set correct data segment }
  82.   MOV  DS,DX
  83.   MOV  DX,PacketCount
  84.   CMP  DX,0
  85.  
  86.   JNZ  @@Exit                  { buffer is not free ! }
  87.  
  88.   MOV  DX,SEG rcvPacket
  89.   MOV  ES,DX
  90.   MOV  DI,OFFSET rcvPacket
  91.  
  92.   MOV  DX,SEG rcvLength
  93.   MOV  DS,DX
  94.   MOV  SI,OFFSET RcvLength
  95.   MOV  WORD PTR DS:[SI],CX     { Store length of frame in PacketLength }
  96.  
  97.   JMP  @@Exit
  98.  
  99. @@FrameCopied:
  100.  
  101.   MOV  DX,SEG PacketCount       { Set correct data segment }
  102.   MOV  DS,DX
  103.   MOV  PacketCount,1            { Set Flag to 1 }
  104.  
  105. @@Exit:
  106.  
  107.   POP  DX                      { Pop registers from stack }
  108.   POP  CX
  109.   POP  BX
  110.   POP  AX
  111. END;
  112. {$S+}
  113.  
  114. FUNCTION ByteToHEXASCII(tByte : Byte) : String;
  115. {╔══════════════════════════════════════════════════════════════════════╗
  116.  ║ FUNCTION  ByteToHEXASCII (...) : String;                             ║
  117.  ╟─────────────────┬────────────────────────────────────────────────────╢
  118.  ║ Description   : │ Converts Byte    to a HEX-ASCII-String             ║
  119.  ║                 │ requests.                                          ║
  120.  ╟─────────────────┼────────────────────────────────────────────────────╢
  121.  ║ Creation date : │ 23-SEPT-93                                         ║
  122.  ╚═════════════════╧════════════════════════════════════════════════════╝}
  123. CONST
  124.    HEXChars: array [0..15] of char = '0123456789ABCDEF';
  125. VAR Nibble1 : Byte;
  126.     Nibble2 : Byte;
  127.     tStr    : String;
  128. BEGIN
  129.   Nibble1 := (tByte AND $0F);        { AND 00001111b }
  130.   Nibble2 := (tByte AND $F0) SHR 4;  { AND 11110000b }
  131.   tStr := HEXChars[Nibble2]+HEXChars[Nibble1];
  132.   ByteToHEXASCII := tStr;
  133. END;
  134.  
  135. FUNCTION WordToHEXASCII(tWord : Word) : String;
  136. {╔══════════════════════════════════════════════════════════════════════╗
  137.  ║ FUNCTION  WordToHEXASCII (...) : String;                             ║
  138.  ╟─────────────────┬────────────────────────────────────────────────────╢
  139.  ║ Description   : │ Converts tWord   to a HEX-ASCII-String             ║
  140.  ║                 │ requests.                                          ║
  141.  ╟─────────────────┼────────────────────────────────────────────────────╢
  142.  ║ Creation date : │ 23-SEPT-93                                         ║
  143.  ╚═════════════════╧════════════════════════════════════════════════════╝}
  144. VAR tStr : String;
  145. BEGIN
  146.   tStr := ByteToHexASCII(Hi(tWord));
  147.   tStr := tStr+ByteToHexASCII(Lo(tWord));
  148.   WordToHexASCII := tStr;
  149. END;
  150.  
  151. {╔══════════════════════════════════════════════════════════════════════╗
  152.  ║ FUNCTION  GetEthernetAddress                                         ║
  153.  ╟─────────────────┬────────────────────────────────────────────────────╢
  154.  ║ Description   : │ Converts a 6 byte ethernet address into a dash     ║
  155.  ║                 │ separated string.                                  ║
  156.  ╟─────────────────┼────────────────────────────────────────────────────╢
  157.  ║ Creation date : │ 23-SEPT-93                                         ║
  158.  ╚═════════════════╧════════════════════════════════════════════════════╝}
  159. FUNCTION GetEthernetAddress(tEtherAddr : EthernetAddr) : String;
  160. VAR tString : String;
  161.     tDigit  : String[02];
  162.     tCount  : Byte;
  163.     AddrLen : Byte;
  164. BEGIN
  165.   AddrLen := SizeOf(EthernetAddr)-1;
  166.   tString := '';
  167.  
  168.   FOR tCount := 0 TO AddrLen DO
  169.   BEGIN
  170.     tString := tString + ByteToHexASCII(tEtherAddr[tCount]);
  171.     IF (tCount < AddrLen) THEN tString := tString + '-';
  172.   END;
  173.   GetEthernetAddress := tString;
  174. END;
  175.  
  176. PROCEDURE TranslateEthernetAddress(tStr : String; VAR tEtheraddr : EthernetAddr);
  177. {╔══════════════════════════════════════════════════════════════════════╗
  178.  ║ PROCEDURE TranslateEthernetAddress(..);                              ║
  179.  ╟─────────────────┬────────────────────────────────────────────────────╢
  180.  ║ Description   : │ Translates a dash separated ethernet address into  ║
  181.  ║                 │ 6 Bytes.                                           ║
  182.  ╟─────────────────┼────────────────────────────────────────────────────╢
  183.  ║ Creation date : │ 23-SEPT-93                                         ║
  184.  ╚═════════════════╧════════════════════════════════════════════════════╝}
  185. VAR tDigit : String;
  186.     tByte  : Byte;
  187.     Err    : Integer;
  188.     tVal   : Byte;
  189. BEGIN
  190.   tDigit :='';
  191.   FOR tByte := 1 TO Length(tStr) DO
  192.   BEGIN
  193.     IF (Copy(tStr,tByte,1) <> '-') THEN tDigit := tDigit + Copy(tStr,tByte,1);
  194.   END;
  195.   FOR tByte := 0 TO 5 DO
  196.   BEGIN
  197.     Val('$'+Copy(tDigit,(tByte*2)+1,2),tVal,Err);
  198.     tEtheraddr[tByte] := tVal;
  199.   END;
  200. END;
  201.  
  202. {╔══════════════════════════════════════════════════════════════════════╗
  203.  ║ FUNCTION SwapWord;                                                   ║
  204.  ╟─────────────────┬────────────────────────────────────────────────────╢
  205.  ║ Description   : │ Swaps the Hi and Lo byte of a word.                ║
  206.  ║                 │                                                    ║
  207.  ╟─────────────────┼──